﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JSMerge {

	public static class StringUtilities {

		/// <summary>
		/// Removes single line comments from the input line.
		/// </summary>
		public static string StripComments( string line ) {
			int commentIndex = line.IndexOf( "//" );
			if ( commentIndex > -1 ) {
				if ( commentIndex > 0 ) {
					line = line.Substring( 0, commentIndex );
				} else {
					line = null;
				}
			}
			return line;
		}

		/// <summary>
		/// Attempts to extract a comment from the line
		/// </summary>
		public static string ExtractComment( string line ) {
			string commentLine = null;
			int commentIndex = line.IndexOf( "//" );
			if ( commentIndex > -1 ) {
				commentLine = line.Substring( commentIndex );
			} else {
				commentIndex = line.IndexOf( "/*" );
				if ( commentIndex > -1 ) {
					int lastIndex = line.Substring( commentIndex ).IndexOf( "*/" );
					if ( lastIndex > -1 ) {
						commentLine = line.Substring( commentIndex, lastIndex );
					} else {
						commentLine = line.Substring( commentIndex );
					}
				}
			}
			return commentLine;
		}

		public static bool IsJSMergeTag( string tag ) {
			if ( tag != null ) {
				StringBuilder sbName = new StringBuilder();
				bool started = false;
				for ( int x = 0; x < tag.Length; x++ ) {
					if ( Char.IsLetter( tag[x] ) ) {
						started = true;
					}
					if ( started ) {
						if ( Char.IsLetter( tag[x] ) || Char.IsNumber( tag[x] ) || tag[x] == '-' ) {
							sbName.Append( tag[x] );
						} else {
							break;
						}
					}
				}
				return sbName.ToString().Equals( Properties.Settings.Default.JSMergeAttributeTag );
			}
			return false;
		}

	}

}
